package com.neuedu.proapi.controller;

import com.neuedu.proapi.pojo.BestUniversities;
import com.neuedu.proapi.service.BestUniversitiesService;
import com.neuedu.proapi.util.ResponseResult;
import lombok.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin(origins = "*")
@RestController
public class BestUniversitiesController {
    @Autowired
    private BestUniversitiesService bestUniversitiesService;
    @GetMapping("/queryAllDFC")
    public ResponseResult<List<BestUniversities>>queryAllDFC(){
        List<BestUniversities> bestUniversities=bestUniversitiesService.queryAllDFC();
        return new ResponseResult<>("200","查询成功",bestUniversities);

    }
    @GetMapping("/queryAllPretty")
    public List<PrettyMap> queryAllPretty() {
        List<BestUniversities> bestUniversities = bestUniversitiesService.queryAllDFC();
        Map<String, List<BestUniversities>> collect = bestUniversities.stream().collect(Collectors.groupingBy(BestUniversities::getProvince));
        List<PrettyMap> prettyMapList = new ArrayList<>();
        collect.forEach((province, list) -> {
            prettyMapList.add(new PrettyMap(province, list.stream().map(BestUniversities::getCitySchool).collect(Collectors.toList())));
        });
        return prettyMapList;
    }
}

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
class PrettyMap {
    private String province;
    private List<String> citySchoolList;
}

